﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Willow.IRepository;
using Willow.IService.Sys;
using Willow.Model;
using Willow.Extensions;
using Willow.Authorization;
namespace Willow.Service.Sys
{
    public class DictionaryItemService : BaseService<Sys_DictionaryItem>, IDictionaryItemService
    {
        public DictionaryItemService(IUnitOfWork unitOfWork)
            : base(unitOfWork)
        {
        }

        /// <summary>
        /// 提交字典项
        /// </summary>
        /// <param name="lstInsert">The LST insert.</param>
        /// <param name="lstDelete">The LST delete.</param>
        /// <param name="lstUpdate">The LST update.</param>
        public void Submit(IEnumerable<Sys_DictionaryItem> lstInsert, IEnumerable<Sys_DictionaryItem> lstDelete, IEnumerable<Sys_DictionaryItem> lstUpdate)
        {
            var currUser = OnlineContext.Instance.GetCurrentUser();

            foreach (var item in lstInsert.OrEmptyIfNull())
            {
                item.Id = Guid.NewGuid();
                item.IsDelete = false;
                item.CreateTime = DateTime.Now;
                item.CreateUserId = currUser.UserId;
                base.Create(item);
            }

            foreach (var item in lstDelete.OrEmptyIfNull())
            {
                var orglItem = base.Find(item.Id);
                orglItem.IsDelete = true;
                orglItem.UpdateTime = DateTime.Now;
                orglItem.UpdateUserId = currUser.UserId;
                base.Update(orglItem);
            }

            foreach (var item in lstUpdate.OrEmptyIfNull())
            {
                var orglItem = this.Find(item.Id);
                orglItem.Name = item.Name;
                orglItem.Remark = item.Remark;
                orglItem.DisplayOrder = item.DisplayOrder;
                orglItem.UpdateTime = DateTime.Now;
                orglItem.UpdateUserId = currUser.UserId;
                base.Update(orglItem);
            }

            UnitOfWork.SaveChanges();
        }
    }
}
